package com.xiaoyg;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.AMQP.BasicProperties.Builder;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @author xiaoyg
 * @Title: Producer
 * @ProjectName rabbitmq-headers
 * @Description: TODO
 * @date 2018/12/510:53
 */
public class Producer {



    private final static String EXCHANGE_NAME = "header-exchange";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();

        connectionFactory.setPort(AMQP.PROTOCOL.PORT);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123456");
        connectionFactory.setHost("192.168.8.129");

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME,"headers",false,true,null);

        String message = new Date().toLocaleString()+" : log something";

        Map<String,Object> headers = new Hashtable<>();
        headers.put("bbb","01234");
        Builder properties = new BasicProperties().builder();

        properties.headers(headers);

        channel.basicPublish(EXCHANGE_NAME,"",properties.build(),message.getBytes());

        System.out.println("send message : '"+message+"'");

        channel.close();
        connection.close();


    }

}
